/**
 * Copyright (c) 2011, www.quartzsource.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.quartzsource.core

class Lava(val config: Config) extends Commands {
  val factories: Map[QURL.Scheme, RepoFactory] = Map()

  def clone(source: QURL, dest: String): QuartzResult = {
    null
  }

  //?? not needed ??
  def parse(path: String, branches: List[String] = Nil): (String, (String, List[String])) = {
    null
  }

  /**
   * Obtain a repository for the specified path
   */
  def repository(env: QEnvironment, path: QURL, create: Boolean = false): Repository = {
    val factory = factories(path.scheme)
    val repository = factory.instantiate(env, path, create)
    Plugins.reposetup(repository)
    repository
  }

  def remoteEnv(src: Repository, options: Map[String, String]): QEnvironment = {
    val dest = src.baseEnvironment.copy // drop repo-specific config
    //copy ssh-specific options
    List("ssh", "remotecmd").foreach(o => {
      val value: Option[String] = options.get(o) match {
        case None => src.environment.config("ui", o)
        case v => v
      }
      value.map(dest.setConfig("ui", o, _))
    })
    //copy selected local settings to the remote ui
    List("auth", "hostfingerprints", "http_proxy").foreach(section => {
      src.environment.config.configItems(section).foreach(t => {
        val (key, value) = t
        dest.setConfig(section, key, value)
      })
    })
    src.environment.config("web", "cacerts").map(dest.setConfig("web", "cacerts", _))
    dest
  }
}

